var selectedRow = '';
var isInit = true;

$(function () {
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openInfoBtnPage();
        }
    }
});

function doRefresh() {
    selectedRow = '';
    var menuId = $('#menuId').val();
    var tabId = $('#tabId').val();
    var status = $('#status').val();
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'pfButton/pageFindButtonData?t=' + new Date().getTime(),
        query: {
            menuId : menuId,
            tabId : tabId,
            status : status
        },
    });
}

function getParams(params) {
    var menuId = $('#menuId').val();
    var tabId = $('#tabId').val();
    var status = $('#status').val();
    var pageSize = params.pageSize;
    var pageNo = params.pageNumber;
    if (isInit) {
        isInit = false;
        pageSize = searchInfo.pageSize ? searchInfo.pageSize : params.pageSize;
        pageNo = searchInfo.pageNo ? searchInfo.pageNo : params.pageNumber;
    }
    return {
        menuId : menuId,
        tabId : tabId,
        status : status,
        pageSize : pageSize,
        pageNo : pageNo
    };
}

function doChooseTab() {
    var menuId = $('#menuId').val();
    if (!menuId || menuId == '') {
        $('#tabId').val('');
        var str = '<option value="">--请选择--</option>';
        $('#tabId').html(str);
        doRefresh();
    } else {
        var postData = {
            menuId : menuId,
        };
        $.ajax({
            type: 'post',
            url: basePath + 'pfButton/selectTab?t=' + new Date().getTime(),
            dataType: 'json',
            data: postData,
            success : function (data) {
                if(data && data.result){
                    setTabSelect(data.content);
                }else if(data&&!data.result){
                    toastr.error(data.message);
                }else{
                    toastr.error('获取城市信息失败');
                }
            },
            error: function (request) {
                delayToastr(common_system_error);
            }
        });
    }
}

function setTabSelect(tabList) {
    $('#tabId').val('');
    var htmlStr = '<option value="">--请选择--</option>';
    if (tabList) {
        for (var i=0; i<tabList.length; i++) {
            htmlStr += '<option value="'+tabList[i].code+'">'+tabList[i].name+'</option>';
        }
    }
    $('#tabId').html(htmlStr);
    doRefresh();
}

function openAddBtnPage() {
    openEditPage('add', 0);
}

function openEditBtnPage() {
    if (!selectedRow || selectedRow == '') {
        toastr.error(common_no_choose_msg.format("编辑"));
        return;
    }
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    openEditPage('edit', selectedRow.id);
}

function openInfoBtnPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    openEditPage('info', selectedRow.id);
}

function openEditPage(command, id) {
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'pfButton/editButtonPage?type=' + command + '&id=' + id + '&' + $('#searchForm').serialize() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function changeState() {
    if (!selectedRow || selectedRow == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var status = 'enabled';
    if (selectedRow.status == 'enabled') {
        status = 'disabled';
    }
    var postData = {
        id : selectedRow.id,
        status : status
    }
    $.ajax({
        type: 'post',
        url: basePath + 'pfButton/changeState?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('修改成功');
                doRefresh();
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error(common_system_error);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function deleteBtn() {
    if (!selectedRow || selectedRow == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var strHtml = '是否删除按钮【'+selectedRow.btnName+'】？该操作会变更按钮数据！';
    swalToastr('', strHtml, 'warning', doBtnDel);
}

function doBtnDel() {
    var postData = {
        id : selectedRow.id,
        status : 'delete'
    };
    $.ajax({
        type: 'post',
        url: basePath + 'pfButton/changeState?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('删除成功');
                doRefresh();
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error(common_system_error);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function formatStatus(value, row, index){
    var title;
    if (value == 'enabled') {
        title = "有效";
    } else {
        title = "无效";
    }
    return title;
}

// dataTable
$('#dataTable').on("click-row.bs.table", function (e, row, $element) {
    // checkBox 的勾选
    $('#dataTable .success').removeClass('success');//去除之前选中的行的，选中样式
    $element.addClass('success');//添加当前选中的 success样式用于区别
    changeStatueBtn(row.status == 'enabled',"冻结","恢复");
    selectedRow = row;
});